package mspbots.cw.business.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import mspbots.cw.common.RabbitKeys;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * com.mspbots.tl.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
@RequiredArgsConstructor
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;

    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }

    @Bean
    public void declareExchange() {

        Exchange exchange = ExchangeBuilder.directExchange(RabbitKeys.BASE_EXCHANGE_NAME).build();
        this.amqpAdmin.declareExchange(exchange);

        Map<String, Object> args = new HashMap<>(1);
        args.put("x-message-ttl", 1000 * 60 * 60 * 24);
        Queue queue = QueueBuilder.durable("connect-wise.bad.message")
                .withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.BAD_MESSAGE_QUEUE_KEY).noargs());

        args = new HashMap<>(2);
        args.put("x-dead-letter-exchange", RabbitKeys.BASE_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", RabbitKeys.BAD_MESSAGE_QUEUE_KEY);

        queue = QueueBuilder.durable("connect-wise.users.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.USERS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.tickets.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.TICKETS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.companies.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.COMPANIES_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.agreements.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
//                .with(RabbitKeys.AGREEMENTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.contacts.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.CONTACTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.time-entries.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.TIME_ENTRIES_SYNC_QUEUE_KEY).noargs());


//        queue = QueueBuilder.durable("connect-wise.priority.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.PRIORITY_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.slas.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.SLAS_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.slas-priority.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.SLAS_PRIORITY_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.teams.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.TEAMS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.in-out-board.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.IN_OUT_BOARD_SYNC_QUEUE_KEY).noargs());


//        queue = QueueBuilder.durable("connect-wise.opportunities.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.OPPORTUNITIES_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.surveys-results.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.SURVEYS_RESULTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.workroles.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.WORKROLES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.worktypes.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.WORKTYPES_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.locations.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.LOCATIONS_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.departments.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.DEPARTMENTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.configurations.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.CONFIGURATIONS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.schedules.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.SCHEDULES_SYNC_QUEUE_KEY).noargs());


//        queue = QueueBuilder.durable("connect-wise.invoices.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.INVOICES_SYNC_QUEUE_KEY).noargs());

//        queue = QueueBuilder.durable("connect-wise.time-sheets.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.TIME_SHEETS_SYNC_QUEUE_KEY).noargs());


//        queue = QueueBuilder.durable("connect-wise.activities.tl").withArguments(args).build();
//        this.amqpAdmin.declareQueue(queue);
//        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
//                .to(exchange).with(RabbitKeys.ACTIVITIES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.orders.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.ORDERS_SYNC_QUEUE_KEY).noargs());


        queue = QueueBuilder.durable("connect-wise.boards.associations.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.ASSOCIATIONS_BOARDS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.boards.tl").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.BOARDS_SYNC_QUEUE_KEY).noargs());


    }
}
